package com.ruoyi.xiaoshou.service.impl;

import com.ruoyi.basedata.api.RemoteGoodsService;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.xiaoshou.domain.SaleRequest;
import com.ruoyi.xiaoshou.domain.SaleRequestDetail;
import com.ruoyi.xiaoshou.dto.AddDto;

import com.ruoyi.xiaoshou.mapper.SaleRequestDetailMapper;
import com.ruoyi.xiaoshou.mapper.SaleRequestMapper;
import com.ruoyi.xiaoshou.service.SaleRequestDetailService;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.security.Security;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class SaleRequestDetailServiceImpl implements SaleRequestDetailService {

    @Resource
    private SaleRequestDetailMapper saleRequestDetailMapper;

    @Resource
    private SaleRequestMapper saleRequestMapper;

    private Logger logger = LoggerFactory.getLogger(SaleRequestDetailServiceImpl.class);
    @Override
    public AjaxResult getList(int orderId) {
        logger.info(" SaleRequestDetailServiceImpl    getList   start...orderId:"+orderId);
        List<SaleRequestDetail> list = saleRequestDetailMapper.getList(orderId);
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult addDetail(AddDto addDto) {
        logger.info(" SaleRequestDetailServiceImpl    addDetail   start...addDto:"+addDto);
        int count = saleRequestDetailMapper.addDetail(addDto);
        if(count>0){
            return AjaxResult.success("添加成功");
        }
        return AjaxResult.success("添加失败");
    }

    @Override
    public AjaxResult addOrderDetail(List<SaleRequestDetail> saleRequestDetail) {
        logger.info(" SaleRequestDetailServiceImpl    addOrderDetail   start..saleRequestDetail:"+saleRequestDetail);
        int orderId = saleRequestDetail.get(0).getOrderId();
        int count = saleRequestDetailMapper.delDetail(orderId);
        Boolean isPrice = true;
        for (SaleRequestDetail requestDetail:saleRequestDetail){
            int count1 = saleRequestDetailMapper.addOrderDetail(requestDetail);
            int  price = saleRequestDetailMapper.getPrice(requestDetail.getProId());
            if(requestDetail.getPrice().compareTo(BigDecimal.valueOf(price))<0){
                //有商品价格低于低价
                isPrice = false;
            }
        }
        SaleRequest saleRequest = new SaleRequest();
        saleRequest.setId(orderId);
        if(isPrice){
            saleRequest.setAuditName("系统");
            saleRequest.setStatus(1);
            //通过系统底价审核，自动审核通过
        }else{
            saleRequest.setAuditName("");
            saleRequest.setStatus(0);
        }
        int count2 = saleRequestMapper.updOrder(saleRequest);
        return AjaxResult.success("添加成功");
    }
}
